/*
 * @(#)NaverCafe.java $version 2013. 3. 11.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package study.android.chajajo.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author zienie
 */
public class NaverCafe {
	
	private static String URL_PREFIX = "http://m.cafe.naver.com/ArticleList.nhn?";
	
	private static String CAFE_KEY = "&search.clubid=";
	private static String MENU_KEY = "&search.menuid=";
	private static String PAGE_KEY = "&search.page=";
	
	
	public static String JOONGONARA = "10050146";
	public static String JOONGONARA_PHONE = "735";
	
	public static String DATA_KEY_LINK_URL = "LINK_URL";
	public static String DATA_KEY_TITLE = "TITLE";
	public static String DATA_KEY_READ = "READ";
	public static String DATA_KEY_NAME = "NAME";
	public static String DATA_KEY_TIME = "TIME";
	
	
	public static List<Map<String, String> > parse(String cafeId, String menuId, int page ) throws Exception{
		
		String url = URL_PREFIX + CAFE_KEY + cafeId + MENU_KEY + menuId + PAGE_KEY + page;  
		Document doc = Jsoup.connect(url).get();
		
		Elements el = doc.getElementsByClass("lst4");
		
		int size = el.size();
		
		System.out.println("url:" + url);
		System.out.println("list size:" + size);
		
		Element listElement = el.get(1);
		
		Elements liList = listElement.getElementsByTag("LI");
		
		System.out.println("size:" + liList.size());
		
		List<Map<String, String> > returnList = new ArrayList<Map<String, String>>();
		
		for(Element li: liList){
			Map<String, String> rowMap = new HashMap<String, String>();
			Elements a = li.getElementsByTag("A");
			if(a == null || a.size() <= 0){
				// LOG
				continue;
			}
			rowMap.put(DATA_KEY_LINK_URL,a.get(0).attr("HREF"));
			
			Elements title = li.getElementsByTag("STRONG");
			if(title == null || title.size() <= 0){
				// LOG
				continue;
			}
			
			Element txt = title.get(0);
			if(txt == null){
				// LOG
				continue;
			}
			//String html = txt.html();
			rowMap.put(DATA_KEY_TITLE, txt.text());
			
			Elements info = li.getElementsByClass("INFO");
			if(info == null || info.size() <= 0){
				// LOG
				continue;
			}
			// "럭키호... | 18:51 | 조회 12"
			StringTokenizer stk = new StringTokenizer(info.get(0).text(), "|");
			String name = "";
            if(stk.hasMoreElements()){
            	rowMap.put(DATA_KEY_NAME, stk.nextToken().replaceAll(" ", ""));
            }
            String time = "";
            if(stk.hasMoreElements()){
            	rowMap.put(DATA_KEY_TIME, stk.nextToken().replaceAll(" ", ""));
            }
            String read = "";
            if(stk.hasMoreElements()){
            	rowMap.put(DATA_KEY_READ, stk.nextToken().replaceAll(" ", "").replaceAll("조회", ""));
            }			
			returnList.add(rowMap);
		}
		return returnList;
	}

}
